package cate.game.client.interceptor;

import cate.game.GameBody;
import cate.game.client.ClientContext;
import cate.game.client.msg.MsgInput;
import cate.game.log.GameLoggerFactory;
import org.slf4j.Logger;

public class CostTimeInterceptor extends AbstractCommandInterceptor{
    @Override
    public void execute(MsgInput msgInput, GameBody gameBody, ClientContext clientContext) {
        long start = System.currentTimeMillis();
        try {
            next.execute(msgInput, gameBody, clientContext);
        } catch (Exception e) {
            Logger log = GameLoggerFactory.getLogger(gameBody, CostTimeInterceptor.class);
            log.error("处理请求{}异常 : ",msgInput.getClass().getName(), e);
        }
        long cost = System.currentTimeMillis() - start;
        if(cost >= 100){
            Logger log = GameLoggerFactory.getLogger(gameBody, CostTimeInterceptor.class);
            log.info("{}请求{} 高消耗时间：{} ms", clientContext.getRole() == null ? "":clientContext.getRole().getBase().name, msgInput.getClass(), cost);
        }
    }
}
